package com.glwan.fixedassetapp.components;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.annotation.Nullable;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.utils.ConditionWrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConditionSearchView extends LinearLayout {

    public Button confirmSearch;
    public Button resetSearch;
    public EditText searchFdYzCardCode;
    public EditText searchFdYzCardName;
    public SelectView searchFdCardCategoryName;
    public EditText searchFdLocation;
    public SelectView searchFdDutyDeptName;
    public Spinner searchStatus;

    private OnButtonClickListener onButtonClickListener;

    private ConditionWrapper conditionWrapper;


    private Map<View, String> columnMap = new HashMap<>();

    public ConditionSearchView(Context context) {
        this(context, null);
    }

    public ConditionSearchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ConditionSearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public ConditionSearchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initData();
    }


    private void initData() {

        conditionWrapper = new ConditionWrapper();
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View inflate = layoutInflater.inflate(R.layout.fgw_offline_product_search_dialog, null);
        inflate.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        //初始化对象
        searchFdYzCardCode = inflate.findViewById(R.id.searchFdYzCardCode);
        searchFdYzCardName = inflate.findViewById(R.id.searchFdYzCardName);
        searchFdCardCategoryName = inflate.findViewById(R.id.searchFdCardCategoryName);
        searchFdLocation = inflate.findViewById(R.id.searchFdLocation);
        searchFdDutyDeptName = inflate.findViewById(R.id.searchFdDutyDeptName);
        searchStatus = inflate.findViewById(R.id.searchStatus);

        //初始化字段map
        columnMap.put(searchFdYzCardCode, "SerialNumber");
        columnMap.put(searchFdYzCardName, "AssetName");
        columnMap.put(searchFdCardCategoryName, "AssetCatalogName");
        columnMap.put(searchFdLocation, "PlaceOfPreservation");
        columnMap.put(searchFdDutyDeptName, "UsingDepartment");
        columnMap.put(searchStatus, "InventoryStatus");


        confirmSearch = inflate.findViewById(R.id.btSearchConfirm);
        //确定事件监听
        confirmSearch.setOnClickListener(view -> {
            conditionValueHandle(false);
            if (onButtonClickListener != null) {
                onButtonClickListener.onButtonCLick(OnButtonClickListener.CONFIRM_BUTTON);
            }
        });

        //重置事件监听
        resetSearch = inflate.findViewById(R.id.btResetSearch);
        resetSearch.setOnClickListener(view -> {
            conditionValueHandle(true);
            if (onButtonClickListener != null) {
                onButtonClickListener.onButtonCLick(OnButtonClickListener.RESET_BUTTON);
            }
        });


        searchStatus.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, new String[]{"全部", "待盘点", "已盘点", "异常盘点"}));

        addView(inflate);
    }

    /**
     * 条件事件处理
     *
     * @param clear 是否为重置
     */
    public void conditionValueHandle(boolean clear) {
        conditionWrapper.resetCondition();
        for (View v : columnMap.keySet()) {
            if (clear) {
                clearConditionValue(v);
            } else {
                setConditionValue(v);
            }
        }
    }

    /**
     * 设置下拉框数据
     *
     * @param name  对象名
     * @param value 值
     */
    public void setSelectValue(String name, String value) {
        if ("fdCardCategoryName".equals(name)) {
            setValue(searchFdCardCategoryName, value);
        } else if ("fdDutyDeptName".equals(name)) {
            setValue(searchFdDutyDeptName, value);
        }
    }

    /**
     * 选择下拉框数据
     *
     * @param name 对象名
     * @param i    索引
     */
    public void setSelectItem(String name, int i) {
        if ("fdCardCategoryName".equals(name)) {
            searchFdCardCategoryName.setSelectedItem(i);
        } else if ("fdDutyDeptName".equals(name)) {
            searchFdDutyDeptName.setSelectedItem(i);
        } else if ("status".equals(name)) {
            searchStatus.setSelection(i);
        }
    }

    /**
     * 设置下拉框数据
     *
     * @param view  view
     * @param value value
     */
    private void setValue(View view, String value) {
        if (!"null".equals(value)) {
            String[] split = value.split(";");
            if (split.length > 0) {
                String[] newSplit = new String[split.length + 1];
                newSplit[0] = "全部";
                System.arraycopy(split, 0, newSplit, 1, split.length);
                if (view instanceof Spinner) {
                    ((Spinner) view).setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, newSplit));
                } else if (view instanceof SelectView) {
                    SelectView selectView = (SelectView) view;
                    selectView.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, newSplit));
                    selectView.setSelectedItem(0);
                }
            }
        }
    }

    /**
     * 相应条件赋值
     *
     * @param view
     */
    private void setConditionValue(View view) {
        String value;
        if (view instanceof EditText) {
            value = ((EditText) view).getText().toString();
            if (value.isEmpty() || "null".equals(value)) {
                return;
            }
            conditionWrapper.like(columnMap.get(view), value);
        } else if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;

            if (spinner.getSelectedItemPosition() == 0) {
                return;
            }
            value = String.valueOf(spinner.getSelectedItem());
            if (view.getId() == R.id.searchStatus) {
                Context context = getContext();
                value = context.getString(R.string.notCounted).equals(value) ? "待盘点" : context.getString(R.string.counted).equals(value) ? "已盘点" : "异常盘点";
            }
            if (value.isEmpty() || "null".equals(value)) {
                return;
            }
            conditionWrapper.eq(columnMap.get(view), value);

        } else if (view instanceof SelectView) {
            SelectView selectView = (SelectView) view;
            if (selectView.getSelectedItemPosition() == 0) {
                return;
            }
            value = String.valueOf(selectView.getSelectTextValue());
            if (value.isEmpty() || "null".equals(value)) {
                return;
            }
            conditionWrapper.eq(columnMap.get(view), value);
        }
    }

    /**
     * 清除条件值
     *
     * @param view
     */
    private void clearConditionValue(View view) {
        if (view instanceof EditText) {
            EditText editText = (EditText) view;
            editText.setText("");
        } else if (view instanceof Spinner) {
            Spinner spinner = (Spinner) view;
            spinner.setSelection(0);
        } else if (view instanceof SelectView) {
            SelectView selectView = (SelectView) view;
            selectView.setSelectedItem(0);
        }
    }

    /**
     * 设置事件按钮监听
     *
     * @param onButtonClickListener onButtonClickListener
     */
    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    /**
     * 返回条件值
     *
     * @return getConditionClause
     */
    public String getConditionClause() {
        return conditionWrapper.getConditionClause();
    }

    /**
     * 返回条件
     *
     * @return getConditionArgs
     */
    public String[] getConditionArgs() {
        List<String> conditionArgs = conditionWrapper.getConditionArgs();
        String[] selectionArgArray;
        if (conditionArgs.size() > 0) {
            selectionArgArray = new String[conditionArgs.size()];
            conditionArgs.toArray(selectionArgArray);
        } else {
            selectionArgArray = new String[0];
        }
        return selectionArgArray;
    }

    /**
     * 按钮点击事件监听类
     */
    public interface OnButtonClickListener {
        Integer CONFIRM_BUTTON = 0;
        Integer RESET_BUTTON = 1;

        void onButtonCLick(int i);
    }
}
